package com.homework;

public class demo08 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 1, 1, 1, 1,};
        for (int i = 0; i < test1(arr, 1).length; i++) {
            System.out.print(test1(arr, 1)[i] + " ");
        }
        System.out.println();
        //测试不存在的情况
        test1(arr, 8);
    }


    public static int[] test1(int[] arr, int num) {
        //计数器
        int count = 0;
        //先遍历数组，得到指定元素在数组中的个数
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                count++;
            }
        }
        //若不存在，给出反馈
        if (count == 0) {
            System.out.println("数组中不存在" + num);
        }
        //定义新数组，接收位置
        int[] arr01 = new int[count];
        //定义索引
        int index = 0;
        //遍历数组，逐一接收
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                arr01[index] = i;
                index++;
            }
        }
        return arr01;
    }
}
